﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Userslib
{
    /*
     * This class provides the data for all the available alliances.
     * Fetches the members,population,highest population and lowest as well.
     * Public static members : 
     * Alliance Dictionary containing alliance number as key and name as value
     * Alliance Stats Dictionary, having the Alliance number as key and as value an instance of localhost.Ally_stats
     * 
     */
    public class Alliances
    {
        private static localhost.GetData Ws = new localhost.GetData();
        private static Dictionary<int, String> alliances = new Dictionary<int, String>();
        private static Dictionary<int,localhost.Alliance_Stats> alliance_stats = new Dictionary<int,localhost.Alliance_Stats>();

        private static Action U = new Action(() =>
        {

            int it = 0;
            foreach (int AID in alliances.Keys)
            {

                try
                {
                    alliance_stats.Add(AID, Ws.Get_Ally_Stats(AID).Cast<localhost.Alliance_Stats>().Where(x => !x.Name.Equals(String.Empty)).FirstOrDefault());// alliance_stats.Add(Ally.Key, Ws.Get_Ally_Stats(Ally.Key).Cast<localhost.Alliance_Stats>().FirstOrDefault());
                }
                catch
                {

                }

                it++;
            }
        });
      
        public static Dictionary<int, String> AllianceDict { get { return alliances; } }
        public static Dictionary<int, localhost.Alliance_Stats> Alliance_Stats { get {
           return alliance_stats; } }

        public static void Refresh() { Task D = Task.Factory.StartNew(U); D.Wait(); }

        public static void Update()
        {

            alliances = Ws.Get_Allies().Cast<localhost.Alliance>().Where(x => !x.Name.Equals(String.Empty)).ToDictionary(x => x.AID, x => { if (x.Name.Length >= 2) return x.Name[0].ToString().ToUpper() + x.Name.ToLower().Substring(1); else return x.Name[0].ToString().ToUpper(); });
            Refresh();


        }

       
    }
}
